<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Role extends MY_Luckydog
{
	public function __construct()
	{
		parent::__construct();
        $this->load->model('luckydog/Role_model', 'role');
        $login_status = $this->login_status = $this->check_login_status();
        if (empty($login_status)){
            redirect('/luckydog/login');
        }
	}
    public function rolelist()
    {
        $_SESSION['v_menu'] = 'role';
        $role_list = $this->role->getRolelList();
        //print_r($role_list);
        $power_arr = array();
        $menuList = $this->config->item("menu");
         if(is_array($role_list))
        {
            foreach ($role_list as $key => $value) 
            {
                $this_power_arr = [];
                $this_power = json_decode($value['power'],true);
                foreach ($this_power as $power => $power_value) {
                    foreach ($power_value as $pkey=>$power) {
                        $this_power_arr[$pkey] = $power;
                    }
                }
                foreach ($menuList as $menu)
                {
                    foreach ($menu['child'] as $childkey => $child)
                    {
                        if(array_key_exists($child['role'], $this_power_arr))
                        {
                            $role_list[$key]['rolePower'][$child['role']]['describe'] = $child['name']; 
                            if(!empty($this_power_arr[$child['role']]))
                            {
                                //print_r($this_power_arr);
                               // print_r($role_list[$key]['rolePower'][$childkey]);die;
                                $role_list[$key]['rolePower'][$child['role']]['child']  = array_intersect(array_keys($child['child']),$this_power_arr[$child['role']]);

                            }
                            
                            
                        }
                    }
                }
                
            }   
        }
        $data = array(
            'role_list'=>$role_list,
            'menuList'=>$menuList
        );

        $this->load->view('/luckydog/systemoption/role_list',$data);
    }

    public function toRolePower(){
        $_SESSION['v_menu'] = 'role';
        $gropuID = $this->input->get("gropuID",true) ? $this->input->get("gropuID",true) : 0;
        $data = array();
        {
            $roleInfo = $this->role->getPowerInfo($gropuID);
            $roleInfo['power'] = json_decode($roleInfo['power'],1);
        }
        $roleList = $this->config->item("menu");
        $data['roleList'] = $roleList;
        $data['roleInfo'] = $roleInfo;
        $data['gropuID'] = $gropuID;
        $this->load->view('/luckydog/systemoption/set_power',$data);
    }


    //角色控制
    public function SystemPowerSet(){

        $gropuID = $this->input->post("gropuID",true);
        $isguanggaozhu = $this->input->post("isguanggaozhu",true);
        $isshanghu= $this->input->post("isshanghu",true);
        $admin = 1;
        if($isshanghu && $isguanggaozhu)
        {
            $admin = 4;
        }
        if($isshanghu == 1 && !$isguanggaozhu)
        {
            $admin = 3;
        }
        if(!$isshanghu && $isguanggaozhu == 1)
        {
            $admin = 2;
        }
        $str = $this->input->post("powerstr",true);
        $rolename = $this->input->post("rolename",true);
        $info = '';
        if(!empty($str)){
            $arr = explode('|',$str); // 拆分最外围的数组
            foreach($arr as $v ){
                $arr2 = array();
                if( -1 < strpos( $v, '#')){
                    $arr2 = explode('#',$v);
                }else{
                    $arr2[] = $v;
                }
                foreach($arr2 as $v2 ){
                    $newarr3 = array();
                    $arr5 = array();
                    $arr4 = array();
                    $arr3 = array();
					$arr6 = [];
                    $arr3 = explode(':', $v2);
                    $arr4 = explode("-", $arr3[1]);
                    foreach($arr4 as $v4){
                        $arr5 = explode('^', $v4);
                       // $newarr3[$arr5['0']] = explode('%', $arr5[1]);
					   $arr6 = explode('~',explode('*',$arr5[1])[0]);
                        $newarr3[$arr5['0']] = $arr6;
						
                    }
					
                    $newarr2[$arr3[0]] = $newarr3;
                }
                $info = $newarr2;
            }
        }

        if($gropuID){
            $data = array(
                "name"=>$rolename,
                "admin"=>$admin,
                "power"=>json_encode($info)
            );
            $res = $this->role->setSystemPower($gropuID,$data);
        }else{
            $data = array(
                "name"=>$rolename,
                "admin"=>$admin,
                "power"=>json_encode($info),
                "addtime"=>time()
            );
            $res = $this->role->setSystemPower("",$data);
        }

        echo $res;
    }

    //删除角色
    public function delRole(){
        $id = $this->input->post("id",true);
        $res = $this->role->delRolebyid($id);
        echo $res;
    }
    //设置角色启用状态
    public function setStatus(){
        $id = $this->input->post("id",true);
        $status = $this->input->post("status",true) ? $this->input->post("status",true) :-1;
        $res = $this->role->setStatus($id,$status);
        echo $res;
    }

}
/* End of file index.php */
/* Location: ./application/controllers/luckydog/role.php */